//
//  AppDelegate.swift
//  BoZeCalendar
//
//  Created by 李东 on 2024/4/10.
//

import UIKit
import IQKeyboardManagerSwift
import WebKit
import AppTrackingTransparency
import BUAdSDK
import AdSupport
@main
class AppDelegate: UIResponder, UIApplicationDelegate {



    var window: UIWindow?
    var bu_openId: BUSplashAd?
    var idfa = ""

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = .white
        window?.makeKeyAndVisible()
        if AppConfig.finishGuide {
            window?.rootViewController = BoZeBaseNavVC(rootViewController: BoZeMainVC())
        }else{
            window?.rootViewController = BoZeGuideVC()
        }
        IQKeyboardManager.shared.enable = true
        if let url = URL(string: "https://www.baidu.com") {
            let request = URLRequest(url: url)
            WKWebView().load(request)
        }
        
        let config = BUAdSDKConfiguration()
        config.appID = ADManager.BUAdAppID
        config.debugLog = 1
        BUAdSDKManager.start(asyncCompletionHandler: { _, _ in
            ADManager.initFinish = true
            DispatchQueue.main.async {
                self.requestIDFA()
                NotificationCenter.default.post(name: .SDKInitFinish, object: nil)
            }
        })   
        return true
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        requestIDFA()
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        adHelper.closeSplashView()
    }
    
    func requestIDFA() {
        if !ADManager.initFinish {
            return
        }
        if #available(iOS 14, *) {
            ATTrackingManager.requestTrackingAuthorization {[weak self] status in
                if status == .authorized {
                    self?.idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString
                 }
                DispatchQueue.main.async {
                    self?.tryToPresentAd()
                }
            }
        } else {
            // Fallback on earlier versions
            if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
                idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString
            }
            DispatchQueue.main.async {
                self.tryToPresentAd()
            }
        }
    }
    lazy var adHelper: ADManager = {
        return ADManager()
    }()
    
    func requestGoogleOpenAd(needLoad: Bool) {
//        appOpenId = nil
//        adHelper.requestGoogleAppOpenAd { openAd in
//            self.appOpenId = openAd
//            self.appOpenId?.fullScreenContentDelegate = self
//            if needLoad {
//                self.tryToPresentAd()
//            }
//        }
    }
    
    
    func tryToPresentAd() {
        if AppProducts.shared.isPurchased {
            adHelper.closeSplashView()
            return
        }
        if AppConfig.isEnglishEnv {//Google
//            if let openAd = appOpenId, let rootVC = window?.rootViewController {
//                openAd.present(fromRootViewController: rootVC)
//            }else {
//                requestGoogleOpenAd(needLoad: true)
//            }
        }else{//穿山甲
            if !idfa.isEmpty {
                BUAdSDKManager.setCustomIDFA(idfa)
            }
            self.adHelper.loadKP()
        }
     
    }

}
